﻿$(document).ready(function () {
    $("#ThoiViecNgayNghiViec").datepicker();

    $("#ThoiViecDialog").dialog({
        autoOpen: false,
        //height: $(window).height() - 30,
        //width: $(window).width() - 60,
        width: 700,
        modal: true,
        buttons:
                {
                    "Hủy thao tác": function () {
                        $(this).dialog("close");
                        return false;
                    },
                    "Lưu": function () {
                        DoSaveThoiViec();
                    }
                }
    });
});

function DoOpenThoiViecDialog(id) {
    var url = basePath + 'NhanVien/GetNhanVienThoiViecAjax/' + id;
    DoOpenBox("boxWaitingForAjax");
    $.getJSON(url, null, function (data) {
        $("#ThoiViecIdNhanVien").val(id);
        $("#ThoiViecSoThe").val(data.SoThe);
        $("#ThoiViecHoTen").val(data.HoTen);
        $("#ThoiViecSoGioDuocHuongThoiViec").val(data.SoGioDuocHuongThoiViec);
        $("#ThoiViecNgayNghiViec").val(data.NgayNghiViec);
        $("#ThoiViecLyDoThoiViec").val(data.LyDoThoiViec);

        BuildBodyTableLichChamCong(data.LichChamCong);

        $("#ThoiViecDialog").dialog("open");
        $("#ThoiViecSoGioDuocHuongThoiViec").focus();
    })
    .error(function (textStatus) {
        DoCheckError_Json(textStatus);
    })
    .complete(function () {
        DoCloseBox("boxWaitingForAjax");
    });
}

function DoShowHideChamCong() {
    if ($("#divChamCong").css("display") == "none") {
        $("#divChamCong").css("display", "block");
    }
    else {
        $("#divChamCong").css("display", "none");
    }
}

function DoSaveThoiViec() {
    if (!confirm("Bạn có chắc chắn muốn lưu?")) return;

    var url = basePath + 'NhanVien/SaveThoiViec';
    DoOpenBox("boxWaitingForAjax");
    $.post(url, $("#ThoiViecForm").serialize(), function (data) {
        alert(data.message);
        window.location.href = document.URL;
    })
    .error(function (textStatus) {
        DoCheckError_Json(textStatus);
    })
    .complete(function () {
        DoCloseBox("boxWaitingForAjax");
    });
}

function BuildBodyTableLichChamCong(data) {
    var strBody = "";
    for (var index = 0; index < data.length; index++) {
        strBody += "\n"
                + "<tr>"
                + " <td><b>" + data[index].ThuTrongTuanString[0].toString().substring(0, 5) + " -> " + data[index].ThuTrongTuanString[6].toString().substring(0, 5) + "<b></td>"
                + " <td title='" + data[index].ThuTrongTuanString[0] + "'>" + data[index].ChamCongNgayTrongTuan[0].KyHieuChamCong + "</td>"
                + " <td title='" + data[index].ThuTrongTuanString[1] + "'>" + data[index].ChamCongNgayTrongTuan[1].KyHieuChamCong + "</td>"
                + " <td title='" + data[index].ThuTrongTuanString[2] + "'>" + data[index].ChamCongNgayTrongTuan[2].KyHieuChamCong + "</td>"
                + " <td title='" + data[index].ThuTrongTuanString[3] + "'>" + data[index].ChamCongNgayTrongTuan[3].KyHieuChamCong + "</td>"
                + " <td title='" + data[index].ThuTrongTuanString[4] + "'>" + data[index].ChamCongNgayTrongTuan[4].KyHieuChamCong + "</td>"
                + " <td title='" + data[index].ThuTrongTuanString[5] + "'>" + data[index].ChamCongNgayTrongTuan[5].KyHieuChamCong + "</td>"
                + " <td title='" + data[index].ThuTrongTuanString[6] + "'>" + data[index].ChamCongNgayTrongTuan[6].KyHieuChamCong + "</td>"
                + "<tr>";
    }
    $("#divChamCong table tbody").html(strBody);
}